var app = getApp();
var api = require("../../utils/api.js");
var util= require("../../utils/libs.js");
var data = require("../../utils/date.js")
Page({
  data: {
    id:null,
    topic:'',
    user:'',
    list:[],
    limit:3,
    page:0,
    shareI:false,
    shareImage:'',
    title: '',
    description:''
  },
  onLoad (options) {
    let that = this
    let uid = wx.getStorageSync('uid')
    that.setData({
      id:options.id,
      uid:uid
    })
    that.topicDetail()
    this.getShare()
    that.topicList()
    // 页面初始化 options为页面跳转所带来的参数
  },
  topicDetail(){
    let that = this
    util.request(api.topicDetail,{
      id:that.data.id,
      uid:that.data.uid
    },
    "GET")
    .then(function (res) {
      if(res.error_code === 0){
        console.log(res)
        let arr = res.data.topic
        if(!isNaN(arr.create_time)){
          arr.create_time = that.getdata(arr.create_time)
        }
        that.setData({
          topic:arr,
          title:arr.title,
          description: arr.description.substring(0,30)+'……',
          user:res.data.topic.user 
        })
      }
    })
  },
  topicList(){
    let that = this
    let uid = wx.getStorageSync("uid")
    util.request(api.topicArticleList,{
      page:that.data.page,
      limit:that.data.limit,
      uid:uid,
      id:that.data.id
    },
    "GET")
    .then(function (res) {
      if(res.error_code === 0){ 
        let arr = res.data.list
        arr.create_time = data.data(arr.create_time)
        that.setData({
          list:arr
        })
      }
    }) 
  },
  toWrting(){
    let that = this
   wx.navigateTo({
     url: "/pages/addwrting/addwrting?id=" + that.data.id + "&&textNumber=" + that.data.topic.text_number
   })
  },
  go(event){
    let id = event.currentTarget.dataset.id
    console.log(id)
    wx.navigateTo({
      url:"/pages/articledetail/articledetail?id="+id
    })
  },
  share() {
    this.setData({
      shareI: true
    })
  },
  closeImg() {
    this.setData({
      shareI: false
    })
  },
  saveI(event) {
    var url = event.currentTarget.dataset.src
    console.log(url)
    wx.getSetting({
      success(res) {
        console.log(res)
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
              wx.downloadFile({
                url: url,
                success(res) {
                  console.log(res)
                  wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success(res) {
                      console.log(res)
                      wx.showToast({
                        title: '保存成功',
                        icon: 'none',
                        duration: 2000
                      })
                      that.setData({
                        shareI: false
                      })
                    },
                    fail(res) {
                      console.log(res)
                    }
                  })
                }
              })
            }
          })
        } else {
          wx.downloadFile({
            url: url,
            success(res) {
              console.log(res)
              wx.saveImageToPhotosAlbum({
                filePath: res.tempFilePath,
                success(res) {
                  console.log(res)
                  wx.showToast({
                    title: '保存成功',
                    icon: 'none',
                    duration: 2000
                  })
                  that.setData({
                    shareI: false
                  })
                },
                fail(res) {
                  console.log(res)
                }
              })
            }
          })
        }
      },
      error(res) {
        console.log(res)
      }
    })
  },
  getShare() {
    var that = this
    wx.request({
      // 获取token
      url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential',
      data: {
        appid: 'wxb78b3211945cae67',
        secret: 'ad9e02f1ea741473fa5615787c0e9d7e'
      },
      success(res) {
        wx.request({
          // 调用接口C
          url: 'https://api.weixin.qq.com/wxa/getwxacode?access_token=' + res.data.access_token,
          method: 'POST',
          data: {
            path: "pages/index/index",
            width: 430,
            auto_color: false,
            line_color: { r: 0, g: 0, b: 0 }
          },
          success(res) {
            let author = ''
            // res是二进制流，后台获取后，直接保存为图片，然后将图片返回给前台
            // 后台二进制怎么转图片？我也不会后台，学会了再贴代码
            if (that.data.user == null) {
              author = '未知'
            } else {
              author = that.data.user.nickname
            }
            util.request(api.upload, {
              type: 2,
              title: that.data.title,
              author: author,
              description: that.data.description
            }, 'POST').then(function (res) {
              that.setData({
                shareImage: res.data.file
              })
            })
          }
        })
      }


    })
  },
  getdata (date) {
    var now = new Date(date*1000);
    var year = now.getFullYear();
    var month = (now.getMonth() + 1) < 10 ? '0' + (now.getMonth() + 1) : (now.getMonth() + 1);
    var date = now.getDate();
    var  hour=now.getHours();     
    var  minute=now.getMinutes();     
    var  second=now.getSeconds();     
    return  month+"-"+date+"   "+hour+":"+minute;
  } 
})
